package com.education.welco.controller;


import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.welco.pojo.*;
import com.education.welco.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 试卷试题 信息操作处理
 *
 * @author d
 * @date 2020-05-12
 */
@RestController
@RequestMapping("/sQuestions")
public class SQuestionsController {

    @Autowired
    private ISQuestionsService questionsService;
    @Autowired
    private ISQuestionsClassService questionsClassService;
    @Autowired
    private ITClassService classService;
    @Autowired
    private ITSchooldeptService schooldeptService;
    @Autowired
    private ISCatalogMenuService catalogMenuService;
    @Autowired
    private ISCatalogService catalogService;
    @Autowired
    private ISCatalogQuestionsService catalogQuestionsService;


    /**
     * 添加课程 试题中间表数据
     */
    @GetMapping("/addCatalogQuestions")
    public String addCatalogQuestions(Integer catalogId){
        SQuestions questions = new SQuestions();
        questions.setCatalogId(catalogId);
        questions.setDelFlag("0");
        List<SQuestions> sQuestions = questionsService.selectSQuestionsList(questions);

        for (SQuestions questions1 : sQuestions) {
            SCatalogQuestions catalogQuestions = new SCatalogQuestions();
            catalogQuestions.setQuestionsId(questions1.getId());
            catalogQuestions.setCatalogId(catalogId);
            catalogQuestionsService.insertSCatalogQuestions(catalogQuestions);
        }
        return "添加成功";
    }


    /**
     *  根据班级查询其部门信息
     */
    public List<TClass> findDept(List<TClass> classList){
        if(classList.size() > 0){
            for (TClass tClass : classList) {
                TSchooldept schooldept = schooldeptService.selectTSchooldeptByClassId(tClass.getDeptId());
                //选择在授班级时上级部门查询错误修改
                if(StringUtils.isNotNull(schooldept)){
                    tClass.setDeptId(schooldept.getId());
                    tClass.setDeptName(schooldept.getDeptName());
                }
            }
        }
        return classList;
    }


    /**
     *  查看班级
     */
    @GetMapping("/answerClass")
    public ApiReturnObject answerClass(Integer catalogId ){
        if(catalogId != null){
            List<TClass> classList = classService.selectTClassByCatalogIdQuestAll(catalogId);
            if(classList != null && classList.size() > 0){
                List<TClass> dept = findDept(classList);
                return new ApiReturnObject("200","查询成功",dept);
            }
            return new ApiReturnObject("200","结果为空",classList);
        }
        return new ApiReturnObject("201","缺少参数","");
    }

    /**
     *  修改查看班级
     */
    @PostMapping("/updateClass")
    public ApiReturnObject updateClass(@RequestBody SQuestions questions){
        //catalogId             classList
        if(questions.getCatalogId() != null ){
            SQuestionsClass questionsClass = new SQuestionsClass();
            questionsClass.setCatalogId(questions.getCatalogId());
            //先删除中间表数据
            questionsClassService.deleteSQuestionsClassByCatalogId(questionsClass.getCatalogId());
            if(questions.getClassList() != null && questions.getClassList().size() > 0){
                questionsClass.setCreateTime(new Date());
                for (TClass tClass : questions.getClassList()) {
                    questionsClass.setClassId(tClass.getId());
                    questionsClassService.insertSQuestionsClass(questionsClass);
                }
            }
            return new ApiReturnObject("200","修改成功","");
        }
        return new ApiReturnObject("201","缺少参数","");
    }

    /**
     *  查看班级  2
     */
    public List<TClass> answerClass2(Integer catalogId ){
        //根据查看内容 查询 查看班级
        List<TClass> classList = classService.selectTClassByCatalogIdQuestAll(catalogId);
        List<TClass> dept = new ArrayList<>();
        if(classList != null && classList.size() > 0){
            dept = findDept(classList);
            return dept;
        }
        return dept;
    }

    /**
     * 查询试卷试题列表
     */
    @GetMapping("/listOther")
    public ApiReturnObject listOther(Integer catalogId,String queType) {
        //  查看班级字符串  查看内容字符串  是否同步  学生查看    名称拼接
        //根据当前id 查询出列表
        if(catalogId != null ){
            SQuestions questions = new SQuestions();
            questions.setCatalogId(catalogId);

            HashMap<String, Object> map = new HashMap<>();
            //班级字符串
            List<TClass> classList = answerClass2(catalogId);
            map.put("classList",classList);
            if(classList != null && classList.size() > 0){
                String s = "";
                int size = classList.size();
                //拼接查看班级字符串
                for (int i = 0; i < size; i++) {
                    if(i == size - 1){
                        //最后一个
                        s = s + classList.get(i).getClassName();
                    }else {
                        s = s + classList.get(i).getClassName() + ",";
                    }
                }
                map.put("classString",s);
            }else {
                map.put("classString","");
            }
            questions.setQueType(queType);
            //资源的分类
            List<SQuestions> sQuestions = questionsService.selectSQuestionsList(questions);
            //给答案前面拼接字母
            for (SQuestions sQuestion : sQuestions) {
                if(sQuestion.getStAnswer() != null && !"".equals(sQuestion.getStAnswer())){
                    //格式正确答案
                    String[] split = sQuestion.getStAnswer().split("");
                    String s = "";
                    if (split.length > 1){
                        for (int i = 0; i < split.length; i++) {
                            if(i != split.length - 1){
                                s = s + split[i] + ",";
                            }else {
                                //最后一个
                                s = s + split[i];
                            }
                        }
                        sQuestion.setStAnswer(s);
                    }
                }
//                sQuestion.setCorrect(new ArrayList<>());
               /* if( !"".equals(sQuestion.getStSelecta())){
                    sQuestion.setStSelecta("A."+ sQuestion.getStSelecta());
                }
                if( !"".equals(sQuestion.getStSelectb())){
                    sQuestion.setStSelectb("B."+ sQuestion.getStSelectb());
                }
                if( !"".equals(sQuestion.getStSelectc())){
                    sQuestion.setStSelectc("C."+ sQuestion.getStSelectc());
                }
                if( !"".equals(sQuestion.getStSelectd())){
                    sQuestion.setStSelectd("D."+ sQuestion.getStSelectd());
                }
                if( !"".equals(sQuestion.getStSelectE())){
                    sQuestion.setStSelectE("E."+ sQuestion.getStSelectE());
                }*/
            }
            if(sQuestions != null && sQuestions.size() > 0 ){
            SQuestions questions1 = sQuestions.get(0);
            //是否同步
            if(questions1.getSynch() != null){
                if("0".equals(questions1.getSynch())){
                    map.put("synchBool",false);
                }else if("1".equals(questions1.getSynch())){
                    map.put("synchBool",true);
                }
            }else {
                map.put("synchBool",false);
            }
            //是否允许学生查看
            if(questions1.getIsStu() != null){
                if("0".equals(questions1.getIsStu())){
                    map.put("stuBool",false);
                }else if("1".equals(questions1.getIsStu())){
                    map.put("stuBool",true);
                }
                }else {
                    map.put("stuBool",false);
                }
            }else {
                map.put("synchBool",false);
                map.put("stuBool",false);
            }
            map.put("sQuestionList",sQuestions);
            return new ApiReturnObject("200","查询成功",map);
        }
        return new ApiReturnObject("201", "缺少catalogId", "");
    }

    /**
     * 查询课后试题
     */
    @GetMapping("/list")
    public ApiReturnObject list(Integer catalogId,String queType, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        HashMap<String, Object> map = Maps.newHashMap();
        if(catalogId != null && queType != null && !"".equals(queType)){
            SQuestions questions = new SQuestions();
            questions.setCatalogId(catalogId);
            questions.setQueType(queType);
            //同步上课资料
            questions.setSynch("1");
            //根据课程  和  问题类型进行查询
            List<SQuestions> sQuestions = questionsService.selectSQuestionsListOther(questions);
            //给答案前面拼接字母
            /*for (SQuestions sQuestion : sQuestions) {
                if( !"".equals(sQuestion.getStSelecta())){
                    sQuestion.setStSelecta("A."+ sQuestion.getStSelecta());
                }
                if( !"".equals(sQuestion.getStSelectb())){
                    sQuestion.setStSelectb("B."+ sQuestion.getStSelectb());
                }
                if( !"".equals(sQuestion.getStSelectc())){
                    sQuestion.setStSelectc("C."+ sQuestion.getStSelectc());
                }
                if( !"".equals(sQuestion.getStSelectd())){
                    sQuestion.setStSelectd("D."+ sQuestion.getStSelectd());
                }
                if( !"".equals(sQuestion.getStSelectE())){
                    sQuestion.setStSelectE("E."+ sQuestion.getStSelectE());
                }
            }*/
            map.put("sQuestions",sQuestions);
            return new ApiReturnObject("200","查询成功",map);
        }
        return new ApiReturnObject("201","缺少catalogId || queType","");
    }


    /**
     * 查询课后试题
     */
    @GetMapping("/listAll")
    public ApiReturnObject listAll(Integer catalogId,String queType, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        if(catalogId != null && queType != null && !"".equals(queType)){
            SQuestions questions = new SQuestions();
            questions.setCatalogId(catalogId);
            questions.setQueType(queType);
            //根据课程  和  问题类型进行查询
            List<SQuestions> sQuestions = questionsService.selectSQuestionsListOther(questions);
            return new ApiReturnObject("200","查询成功",sQuestions);
        }
        return new ApiReturnObject("201","缺少catalogId || queType","");
    }


    /**
     * 新增课后试题
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SQuestions questions) {
        if(questions.getCatalogId() != null){
            questions.setIsStu("0");
            questions.setSynch("0");
            questions.setCreateTime(new Date());
            questionsService.insertSQuestions(questions);

            //中间表添加数据
            SCatalogQuestions catalogQuestions = new SCatalogQuestions();
            catalogQuestions.setQuestionsId(questions.getId());
            catalogQuestions.setCatalogId(questions.getCatalogId());
            catalogQuestionsService.insertSCatalogQuestions(catalogQuestions);

            return new ApiReturnObject("200","添加成功","");
        }
        return new ApiReturnObject("201","缺少catalogId","");

    }

    /**
     *  获取到最上面一级的id     查看班级列表 课程分享班级
     */
    public Integer GetFirstId2(Integer catalogId){
        SCatalogMenu sCatalogMenu = catalogMenuService.selectSCatalogMenuById(catalogId);
        //第二级
        if(sCatalogMenu.getParentId() != 0){
            //第一级
            SCatalogMenu sCatalogMenu1 = catalogMenuService.selectSCatalogMenuById(sCatalogMenu.getParentId());
            return GetFirstId2(sCatalogMenu1.getId());
        }
        return sCatalogMenu.getId();
    }

    /**
     *  修改此门备课的状态
     */
    public SCatalogMenu changeStatus(SQuestions questions){
        //将资源所在的备课状态改变
        Integer rCatalogId = questions.getCatalogId();
        //教师创建课程表
        SCatalogMenu catalogMenu1 = catalogMenuService.selectSCatalogMenuById(rCatalogId);
        //目录最下面一级
        Integer parentId = catalogMenu1.getParentId();
        //备课课程id
        Integer integer = GetFirstId2(parentId);
        SCatalogMenu catalogMenu = catalogMenuService.selectSCatalogMenuById(integer);
        return catalogMenu;
    }

    public SQuestions updateStatus(SQuestions questions){
        //app 端查看 允许
        String i = "1";
        if(i.equals(questions.getIsStu())){
            SCatalogMenu catalogMenu = changeStatus(questions);
            if(!i.equals(catalogMenu.getIsStu())){
                //修改备课是否允许学生查看状态
                catalogMenu.setIsStu("1");
                catalogMenuService.updateSCatalogMenu(catalogMenu);
            }
        }
        // 同步到上课资料
        if(i.equals(questions.getSynch())){
            //同步上课资料
            SCatalogMenu catalogMenu = changeStatus(questions);
            if(!i.equals(catalogMenu.getIsSyn())){
                //修改同步上课资料状态
                catalogMenu.setIsSyn("1");
                catalogMenuService.updateSCatalogMenu(catalogMenu);
            }
        }
        return questions;
    }


    /**
     * 修改课程预习的状态
     */
    @PostMapping("/updateData")
    public ApiReturnObject updateData(@RequestBody SQuestions questions) {
        if(questions.getId() != null ){
            questions.setCreateTime(new Date());
            questionsService.updateSQuestions(questions);
            return new ApiReturnObject("200","修改成功","");
        }
        return new ApiReturnObject("201","缺少参数id","");
    }

    /**
     * 修改课程预习的状态
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SQuestions questions) {
//        if(catalogPreview.getId() != null){
        //修改某个字段 避免空值报错 针对于查看内容中的课程问答状态
        //修改 查看内容 状态  修改上级
        if(questions.getCatalogId() != null){
            updateStatus(questions);
        }
        //修改状态
        if(questions.getSQuestionList() != null && questions.getSQuestionList().size() >0 ){
            for (SQuestions sQuestions : questions.getSQuestionList()) {
                if(questions.getIsStu() != null && !"".equals(questions.getIsStu())){
                    sQuestions.setIsStu(questions.getIsStu());
                }
                if(questions.getSynch() != null && !"".equals(questions.getSynch())){
                    sQuestions.setSynch(questions.getSynch());
                }
                questionsService.updateSQuestions(sQuestions);
            }
            return new ApiReturnObject("200","修改成功","");
        }else {
            return new ApiReturnObject("202","请选择要修改的内容","");
        }
    }



    /**
     * 删除课后试题
     */
    @PostMapping("/del")
    public ApiReturnObject remove(@RequestBody SQuestions questions) {
        if(questions.getId() != null){
            //查询内容是否分享
            SQuestions questions1 = questionsService.selectSQuestionsById(questions.getId());
            if("1".equals(questions1.getIsStu()) || "1".equals(questions1.getSynch()) ){
                return new ApiReturnObject("201","该内容已被分享，无法删除","");
            }
            questionsService.deleteSQuestionsById(questions.getId());
            return new ApiReturnObject("200","删除成功","");
        }
        return new ApiReturnObject("201","缺少id","");
    }


}